<?php

class __festival extends __basic_def{
	
	protected $_cache_file;
	function __construct(){
		$this->_cache_file = DATA_PATH. "/_festival.php";
		// 如果文件建立超过30天则重新建立
		if(file_exists($this->_cache_file)){
			$file_create_time = filectime($this->_cache_file);
			if(false !== $file_create_time){
				if(time()-$file_create_time > 86400*30) unlink($this->_cache_file);
			}
		}
		
		// 判断节日缓存数据是否存在，如果不在则新建
		if(!file_exists($this->_cache_file)){
			$conn = init_db();
			$d1 = dateadd("day", -90, time());
			$d2 = dateadd("day", 399, time());
			$row = $conn->get_rows("select * from weather_festival where type in(0,1,3) or (type=2 and UNIX_TIMESTAMP(date)>={$d1} and UNIX_TIMESTAMP(date)<={$d2}) order by date");
			$row1 = array();
			foreach($row as $r){
				if($r["type"] == 0) $row1[0][$r["date"]][] = array("beginyear"=>$r["beginyear"], "title"=>$r["title"], "content"=>$r["content"]);
				if($r["type"] == 1) $row1[1][$r["date"]][] = array("title"=>$r["title"], "content"=>$r["content"]);
				if($r["type"] == 2) $row1[2][$r["date"]][] = array("title"=>$r["title"], "content"=>$r["content"]);
				if($r["type"] == 3) $row1[3][$r["date"]][] = array("beginyear"=>$r["beginyear"], "title"=>$r["title"], "content"=>$r["content"]);
			}
			$row = array(
					"0" => "\$_festival_fixed = array(\r\n",	// 固定节日
					"1" => "\$lunar_holiday = array(\r\n",		// 农历节日
					"2" => "\$lunar_almanac = array(\r\n",		// 黄历
					"3" => "\$_festival = array(\r\n",		// 不固定节日
			);
			foreach($row1 as $key=>$row2){
				foreach ($row2 as $k=>$row3){
					$row[$key] .= "	\"{$k}\" => array(\r\n";
					foreach($row3 as $r){
						$r["content"] = str_replace("\"", "“", $r["content"]);
						if($key==1 || $key==2)
							$row[$key] .= "		array(\"title\"=>\"$r[title]\", \"content\"=>\"$r[content]\"),\r\n";
						else
							$row[$key] .= "		array(\"beginyear\"=>\"$r[beginyear]\", \"title\"=>\"$r[title]\", \"content\"=>\"$r[content]\"),\r\n";
					}
					$row[$key] .= "	),\r\n";
				}
			}
			$row[0] .= ");\r\n";
			$row[1] .= ");\r\n";
			$row[2] .= ");\r\n";
			$row[3] .= ");\r\n";
			// 写入缓存文件
			$fp = fopen($this->_cache_file, "w");
			if(!$fp) die("System error!");
			fwrite($fp, "<?php\r\n");
			fwrite($fp, $row[0]. "\r\n");
			fwrite($fp, $row[1]. "\r\n");
			fwrite($fp, $row[2]. "\r\n");
			fwrite($fp, $row[3]. "\r\n");
			fwrite($fp, "\r\n?>");
			fclose($fp);
		}
	}
	
	/**
	 * 获取节日数据
	 * @param timestamp $dateline
	 * @return array:
	 */
	public function get($dateline){
		global $_festival_fixed, $lunar_holiday, $lunar_almanac, $_festival;
		require_once $this->_cache_file;
		
		$day_info = array();
		
		// 获取农历节气
		$solar = init_solar();
		$s = $solar->get_solar_term($dateline);
		$s_arr = explode("|", $s);
		
		// 获取农历
		$lunar = init_lunar();
		$ldate = $lunar->convertSolarToLunar(date("Y", $dateline), date("m", $dateline), date("d", $dateline));
		// 暂时解决下拉显示不完全的问题
		if(strlen($s_arr[0])) {
			$day_info["lunar"] = $ldate[3] . "年 " . $ldate[1] . $ldate[2] . " {$s_arr[0]}";
		}else{
			$day_info["lunar"] = $ldate[3] . "(" . $ldate[6] . ")年 " . $ldate[1] . $ldate[2];
		}
		
		// 如果农历节气有介绍
		if(isset($s_arr[1]) && strlen($s_arr[1])){
			$day_info["solar_festival"][] = array("beginyear"=>0, "title"=>$s_arr[0], "content"=>$s_arr[1]);
		}
		
		// 获取农历节日
		if(strlen($ldate[4])==1) $ldate[4] = "0".$ldate[4];
		if(strlen($ldate[5])==1) $ldate[5] = "0".$ldate[5];
		if(isset($lunar_holiday[$ldate[4].$ldate[5]])){
			$day_info["lunar_festival"] = $lunar_holiday[$ldate[4].$ldate[5]];
		}
		
		// 获取不固定节日
		// 获取是星期几，w参数获取出来为0-6，为了方便我们给它加上1变成1=7比较符合习惯
		$week = date("w", $dateline);	// 星期几
		// 获取第几个
		$num = ceil(date("j", $dateline)/7);
		foreach($_festival as $key=>$val){
			if($key == date("n", $dateline). ",{$num},{$week}"){
				$day_info["festival_unstable"][] = $val[0];
			}
		}
		// 获取固定节日
		$s = isset($_festival_fixed[date("md", $dateline)]) ? $_festival_fixed[date("md", $dateline)] : "";
		if(is_array($s)) $day_info["festival_fixed"] = $s;
		// 获取黄历
		$s = array();
		foreach($lunar_almanac[date("Y-m-d", $dateline)] as $r){
			$s[] = "$r[title] : $r[content]";
		}
		$day_info["almanac"] = $s;
		
		return $day_info;
	}
	
	
	
	
	
	
}